package com.quant.config;

import com.quant.service.DataService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Configures cross-domain processing for the application.
 *
 * @author lu
 *
 */
@Configuration
public class MyAutoConfiguration implements WebMvcConfigurer {

	public MyAutoConfiguration(DataService dataService) {
		this.dataService = dataService;
	}

	private final DataService dataService;
	/**
	 * This method is part of the Spring Web MVC framework and is used to add CORS (Cross-Origin Resource Sharing) mappings.
	 *
	 * @param registry The CorsRegistry object used to define CORS mappings.
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedOriginPatterns("*")
				.allowedHeaders("*")
				.allowedMethods("*")
				.allowCredentials(true)
				.maxAge(3600);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**")
				.addResourceLocations("classpath:/static/");

	}


	@Bean
	public void init() {
		dataService.initTables();
	}

}
